package com.hzw.controller;

import com.hzw.bean.Article;
import com.hzw.bean.FriendsRelation;
import com.hzw.bean.LikeRelation;
import com.hzw.bean.User;
import com.hzw.dto.ApiResult;
import com.hzw.dto.TranspondParam;
import com.hzw.service.*;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/3.
 */

@Controller
@CrossOrigin
@RequestMapping(value = "/friendsRelation")
public class FriendsRelationController {
    @Resource
    private FriendsRelationService friendsRelationService;
    @Resource
    private UserService userService;
    @Resource
    private ArticleService articleService;
    @Resource
    private MessageService messageService;
    @Resource
    private LikeRelationService likeRelationService;

    /**
     * 获取是否已关注标志位
     * @param friendsRelation
     * @return
     */
    @RequestMapping(value = "/getFocusFlag",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getFocusFlag(@RequestBody FriendsRelation friendsRelation){
        ApiResult result = new ApiResult();
        String userId = friendsRelation.getUserId();
        String targetId = friendsRelation.getTargetId();
        if(ObjectUtils.isNullOrEmpty(userId) || ObjectUtils.isNullOrEmpty(targetId)){
            result.error(CLIENT_ERROR,"缺少参数：userId或targetId");
            return result;
        }else{
            try{
                int focusFlag = friendsRelationService.getFocusFlag(userId,targetId);
                result.setData(focusFlag);
                result.setMsg("0表示未关注，1表示已关注");
            }catch (Exception e){
                result.error(SERVER_ERROR,"获取失败，原因："+e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 新增关注
     * @param friendsRelation
     * @return
     */
    @RequestMapping(value = "/addFocus",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addFocus(@RequestBody FriendsRelation friendsRelation){
        ApiResult result = new ApiResult();
        String userId = friendsRelation.getUserId();
        String friendId = friendsRelation.getTargetId();
        if(ObjectUtils.isNullOrEmpty(userId) || ObjectUtils.isNullOrEmpty(friendId)){
            result.error(CLIENT_ERROR,"缺少参数：userId或friendId");
            return result;
        }else{
            try{
                friendsRelationService.addFocus(friendsRelation);
                result.setMsg("关注成功！");
            }catch (Exception e){
                result.error(SERVER_ERROR,"关注失败，原因："+e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 取消关注
     * @param friendsRelation
     * @return
     */
    @RequestMapping(value = "/cancelFocus",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult cancelFocus(@RequestBody FriendsRelation friendsRelation){
        ApiResult result = new ApiResult();
        String userId = friendsRelation.getUserId();
        String friendId = friendsRelation.getTargetId();
        if(ObjectUtils.isNullOrEmpty(userId) || ObjectUtils.isNullOrEmpty(friendId)){
            result.error(CLIENT_ERROR,"缺少参数：userId或friendId");
            return result;
        }else{
            try{
                friendsRelationService.cancelFocus(friendsRelation);
                result.setMsg("取消关注成功！");
            }catch (Exception e){
                result.error(SERVER_ERROR,"取消关注失败，原因："+e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 获取关注人列表
     * @param friendsRelation
     * @return
     */
    @RequestMapping(value = "/selectFocusUserById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectFocusUserById(@RequestBody FriendsRelation friendsRelation){
        ApiResult result = new ApiResult();
        String userId = friendsRelation.getUserId();
        if(ObjectUtils.isNullOrEmpty(userId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId");
        }else {
            try{
                List<User> userList = userService.selectFocusUserById(userId);
                if (!ObjectUtils.isNullOrEmpty(userList)){
                    result.setMsg("查找关注人列表成功！");
                    result.setData(userList);
                }else {
                    result.setMsg("关注人列表无数据");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 获取粉丝列表
     * @param friendsRelation
     * @return
     */
    @RequestMapping(value = "/selectFollowUserById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectFollowUserById(@RequestBody FriendsRelation friendsRelation){
        ApiResult result = new ApiResult();
        String userId = friendsRelation.getUserId();
        if(ObjectUtils.isNullOrEmpty(userId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId");
        }else {
            try{
                List<User> userList = userService.selectFollowUserById(userId);
                if (!ObjectUtils.isNullOrEmpty(userList)){
                    result.setMsg("查找粉丝列表成功！");
                    result.setData(userList);
                }else {
                    result.setMsg("粉丝列表无数据");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 获取点赞关系标志位
     */
    @RequestMapping(value = "/getLikeFlag",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getLikeFlag(@RequestBody Article article){
        ApiResult result = new ApiResult();
        String userId = article.getUserId();  //点赞者的Id
        String articleId = article.getId(); //被点赞的文章Id
        if(ObjectUtils.isNullOrEmpty(userId)|| ObjectUtils.isNullOrEmpty(articleId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId或articleId");
        }else {
            try{
                LikeRelation likeRelation = likeRelationService.selectByTwoId(userId,articleId);
                if(likeRelation == null){
                    result.setData(0);
                }else {
                    result.setData(likeRelation.getLikeFlag());
                }
                result.setMsg("查询点赞标志位成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 点赞博文
     * @param article
     * @return
     */
    @RequestMapping(value = "/addLike",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addLike(@RequestBody Article article){
        ApiResult result = new ApiResult();
        String userId = article.getUserId();  //点赞者的Id
        String articleId = article.getId(); //被点赞的文章Id
        if(ObjectUtils.isNullOrEmpty(userId)|| ObjectUtils.isNullOrEmpty(articleId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId或articleId");
        }else {
            try{
                articleService.addLike(userId,articleId);
                result.setMsg("点赞成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 取消点赞博文
     * @param article
     * @return
     */
    @RequestMapping(value = "/cancelLike",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult cancelLike(@RequestBody Article article){
        ApiResult result = new ApiResult();
        String userId = article.getUserId();  //点赞者的Id
        String articleId = article.getId(); //被点赞的文章Id
        if(ObjectUtils.isNullOrEmpty(userId)|| ObjectUtils.isNullOrEmpty(articleId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId或articleId");
        }else {
            try{
                articleService.cancelLike(userId,articleId);
                result.setMsg("取消点赞成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 转发博文
     * @param param
     * @return
     */
    @RequestMapping(value = "/transpondArticle",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult transpondArticle(@RequestBody TranspondParam param){
        ApiResult result = new ApiResult();
        String transponUserId = param.getTransponUserId(); // 转发者的id
        if(ObjectUtils.isNullOrEmpty(transponUserId) || ObjectUtils.isNullOrEmpty(param.getArticle())){
            return result.error(CLIENT_ERROR,"缺少参数：transponUserId");
        }

        //获取用户活跃度，检查是否有权限转发
        User user = userService.selectUserById(transponUserId);
        if(user.getMemberFlag() == 0){
            return result.error(CLIENT_ERROR,"对不起，您不是会员，没有权限转发博文！");
        } else if(user.getMemberFlag() == 1){
            try{
                articleService.transpondArticle(transponUserId,param.getArticle());
                result.setMsg("转发成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }

        return result;
    }

    /**
     * 举报博文
     * @param param
     * @return
     */
    @RequestMapping(value = "/chargeArticle",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult chargeArticle(@RequestBody TranspondParam param){
        ApiResult result = new ApiResult();
        String transponUserId = param.getTransponUserId(); // 举报者的id
        if(ObjectUtils.isNullOrEmpty(transponUserId) || ObjectUtils.isNullOrEmpty(param.getArticle())){
            return result.error(CLIENT_ERROR,"缺少参数：transponUserId");
        }

        //获取用户活跃度，检查是否有权限举报
        User user = userService.selectUserById(transponUserId);
        if(user.getMemberFlag() == 0){
            return result.error(CLIENT_ERROR,"对不起，您不是会员，没有权限举报博文！");
        } else if(user.getMemberFlag() == 1){
            try{
                articleService.chargeArticle(transponUserId,param.getArticle());
                result.setMsg("举报成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }

        return result;
    }

}
